package com.ifighter.activity;

import java.sql.SQLException;

import com.ifighter.R;
import com.ifighter.helper.MyDbHelper;
import com.ifighter.service.user.User;
import com.ifighter.service.user.UserService;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	public EditText nickname;
	public EditText password;
	public MyDbHelper myDbHelper;
	private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		myDbHelper = new MyDbHelper(MainActivity.this);
		db         = this.myDbHelper.getReadableDatabase();

		myDbHelper.onCreate(db);

		Button login  = (Button) findViewById(R.id.btnLogin);
		Button signUp = (Button) findViewById(R.id.btnSignUp);

		nickname = (EditText) findViewById(R.id.editTextNickname);
		password = (EditText) findViewById(R.id.editTextPassword);

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				User user               = null;
				UserService userService = new UserService(db);
				String sNickname        = nickname.getText().toString();
				String sPassword        = password.getText().toString();

				try {
					user = userService.getUserByCredential(sNickname, sPassword);

					if(user != null){

					} else {
						Toast.makeText(MainActivity.this, "Credenziali errate", Toast.LENGTH_LONG).show();
					}

				} catch (SQLException e) {

				}
			}
		});

		signUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
				startActivity(intent);
			}
		});
	}
}